Skip to content

Conversation

SamMousa
Copy link
Contributor

  • Switch to esbuild for CSS
  • Use Vanilla CSS source
  • Switch to esbuild for JS and bundled JS
  • Remove lots of dev deps
  • Remove complex manual build scripts in favor of clear and readable esbuild scripts, example here:

From esbuild.bundle.mjs:

import esbuild from 'esbuild';
import package_json from '../package.json' with { type: "json" };

async function compileCssToJs(esbuild, path) {
  const compiledCss = await esbuild.build({
    bundle: true,
    entryPoints: [path],
    loader: {
      '.svg': 'dataurl',
    },
    minify: true,
    write: false
  });

  if (compiledCss.outputFiles.length !== 1) {
    throw "Expected one css output file";
  }
  const css = JSON.stringify(compiledCss.outputFiles[0].text);

  const script = `
    (() => {
        const style = document.createElement('style');
        style.innerHTML = ${css};
        document.head.append(style);
    })()
  `
  return script;
}

const jsBanner = `
/**
 * WinBox.js Bundle (${package_json.version})
 * Author and Copyright: Thomas Wilkerling
 * Licence: Apache-2.0
 * Hosted by Nextapps GmbH
 * https://github.com/nextapps-de/winbox
 */
`
esbuild.build({
    bundle: true,
    outfile: "dist/winbox.bundle.min.js",
    minify: true,
    stdin: {
        contents: `
        import WinBox from './winbox';
        import '../css/winbox.css';
        module.exports = WinBox;
        `,
        resolveDir: "src/js"
    },
    loader: {
      '.svg': 'dataurl',
    },
    banner: {
      js: jsBanner
    },
    plugins: [
      {
        name: "Import CSS",
        setup(build) {
          build.onLoad({ filter: /\.css$/}, async (args) => {
            if (args.namespace !== 'file') {
              return;
            }
            return {
                contents: await compileCssToJs(build.esbuild, args.path),
                loader: 'js'
            }
          })
        }
    }
    ],
    globalName: "WinBox",
    metafile: true
  });

Review thorougly, this will probably break my other PR, which I'll rebase after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant